package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SignAdapter;
import flc.ast.bean.WatchedBean;
import flc.ast.databinding.ActivitySignBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkResSetBean;

public class SignActivity extends BaseAc<ActivitySignBinding> {

    public static int type;
    private SignAdapter signAdapter;
    private String key;

    @Override
    protected int onCreate() {
        return R.layout.activity_sign;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSignBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivSearch.setOnClickListener(this);

        mDataBinding.rvSignList.setLayoutManager(new LinearLayoutManager(mContext));
        signAdapter = new SignAdapter();
        mDataBinding.rvSignList.setAdapter(signAdapter);
        signAdapter.addChildClickViewIds(R.id.tvSignItemSign);
        signAdapter.setOnItemClickListener(this);
        signAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSearch://搜索
                key = mDataBinding.etSearch.getText().toString().trim();
                if (key.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_movie_name));
                    return;
                }
                getData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        List<WatchedBean> listSpWatched = AppSpUtils.getWatchedList();
        List<StkResBean> listSp = type == 1 ? AppSpUtils.getWantWatchList() : AppSpUtils.getWatchingList();
        if (listSpWatched == null) listSpWatched = new ArrayList<>();
        if (listSp == null) listSp = new ArrayList<>();
        switch (view.getId()) {
            case R.id.tvSignItemSign://追剧
                if (signAdapter.getItem(position).isSelected()) {
                    if (type == 0) {
                        for (WatchedBean listDatum : listSpWatched) {
                            if (listDatum.getBean().getUrl().equals(signAdapter.getItem(position).getUrl())) {
                                listSp.remove(listDatum);
                                break;
                            }
                        }
                    } else {
                        for (StkResBean listDatum : listSp) {
                            if (listDatum.getUrl().equals(signAdapter.getItem(position).getUrl())) {
                                listSp.remove(listDatum);
                                break;
                            }
                        }
                    }
                } else {
                    if (type == 0) {
                        listSpWatched.add(0, new WatchedBean(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")), signAdapter.getItem(position)));
                    } else {
                        listSp.add(0, signAdapter.getItem(position));
                    }
                }
                if (type == 0) {
                    AppSpUtils.saveWatchedList(listSpWatched);
                } else if (type == 1) {
                    AppSpUtils.saveWantWatchList(listSp);
                } else if (type == 2) {
                    AppSpUtils.saveWatchingList(listSp);
                }
                signAdapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    protected void initData() {
        if (type == 0) {
            mDataBinding.etSearch.setHint(getString(R.string.watched_sign));
        } else if (type == 1) {
            mDataBinding.etSearch.setHint(getString(R.string.want_watch_sign));
        } else if (type == 2) {
            mDataBinding.etSearch.setHint(getString(R.string.watching_sign));
        }
    }

    private void getData() {
        StkResApi.getTagResourceSetsList(null, AppConfig.SEARCH_DATA, key, null, false, new IReqRetCallback<StkResSetBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable StkResSetBean result) {
                if (success) {
                    if (result.articleList.size() > 0) {
                        signAdapter.setList(result.articleList);
                        signAdapter.setType(type);
                        mDataBinding.rvSignList.setVisibility(View.VISIBLE);
                        mDataBinding.ivSearchNoData.setVisibility(View.GONE);
                    } else {
                        mDataBinding.rvSignList.setVisibility(View.GONE);
                        mDataBinding.ivSearchNoData.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

}